import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-21
 * Time: 10:08
 */
class Node {
    public int val;//数据域
    public Node next;//指针域指向下一个元素

    //构造方法
    public Node (int val){
        this.val = val;
    }
}
public class MyQueue {
    public Node head;//头结点 - 出队
    public Node last;//尾结点 - 入队

    /**
     * 入队
     * @param val 数据
     */
    public void offer(int val){
        Node node = new Node(val);//生成新节点

        if (head == null){//判断当前是否为空队列
            head = node;
            last = node;
        }else{//不为空，正常尾指针后移
            last.next = node;
            last = node;
        }
    }

    /**
     * 出队
     * @return 队头元素
     */
    public int poll(){
        if (isEmpty()){//判断队列是否为空 - 为空则抛异常
            throw new RuntimeException("队列为空");
        }
        int oldVal = head.val;
        this.head = this.head.next;//头指针后移
        return oldVal;
    }

    public boolean isEmpty(){
        return this.head == null;//直接返回表达式的布尔值即可
    }

    public int peek(){
        if(isEmpty()){//判空
            throw  new RuntimeException("队列为空");
        }
        return head.val;//返回头结点值
    }
}
